<template>
  <view class="jnpf-wrap jnpf-wrap-workflow">
    <u-form
      :model="dataForm"
      ref="dataForm"
      :errorType="['toast']"
      label-position="left"
      label-width="200"
      label-align="left"
    >
      <u-form-item label="委外进货单别">
        <u-input v-model="dataForm.th001" disabled></u-input>
      </u-form-item>
      <u-form-item label="委外进货单号">
        <u-input v-model="dataForm.th002" disabled></u-input>
      </u-form-item>
      <u-form-item label="打印次数">
        <u-input v-model="dataForm.th025" disabled></u-input>
      </u-form-item>
      <u-form-item label="单据日期">
        <u-input v-model="dataForm.th029" disabled></u-input>
      </u-form-item>
      <u-form-item label="工厂">
        <u-input v-model="dataForm.th004" disabled></u-input>
      </u-form-item>
      <u-form-item label="委外供应商">
        <u-input v-model="dataForm.th005" disabled></u-input>
      </u-form-item>
      <u-form-item label="部门">
        <u-input v-model="dataForm.th038" disabled></u-input>
      </u-form-item>
      <u-form-item label="签核状态码">
        <u-input v-model="dataForm.th036" disabled></u-input>
      </u-form-item>
      <u-form-item label="项目编号">
        <u-input v-model="dataForm.th045" disabled></u-input>
      </u-form-item>

      <u-form-item label="QC人员编号">
        <u-input v-model="dataForm.udf11" disabled></u-input>
      </u-form-item>
      <u-form-item label="QC人员名称">
        <u-input v-model="dataForm.udf12" disabled></u-input>
      </u-form-item>
      <u-form-item label="附件">
        <jnpf-file v-model="dataForm.attachmentUpload" />
      </u-form-item>
      <u-tabs-swiper
        ref="tabs"
        :list="swiperList"
        :current="current"
        @change="tabsChange"
        :is-scroll="false"
      ></u-tabs-swiper>
      <view v-show="current == 0">
        <u-form-item label="发票种类">
          <u-input v-model="dataForm.th012" disabled></u-input>
        </u-form-item>
        <u-form-item label="发票日期">
          <u-input v-model="dataForm.th013" disabled></u-input>
        </u-form-item>
        <u-form-item label="发票号码">
          <u-input v-model="dataForm.th014" disabled></u-input>
        </u-form-item>
        <u-form-item label="税号">
          <u-input v-model="dataForm.th011" disabled></u-input>
        </u-form-item>
      </view>
      <view v-show="current == 1">
        <u-form-item label="币种">
          <u-input v-model="dataForm.th007" disabled></u-input>
        </u-form-item>
        <u-form-item label="汇率">
          <u-input v-model="dataForm.th008" disabled></u-input>
        </u-form-item>
        <u-form-item label="件数">
          <u-input v-model="dataForm.th009" disabled></u-input>
        </u-form-item>
        <u-form-item label="自动扣料更新码">
          <u-input v-model="dataForm.th026" disabled></u-input>
        </u-form-item>
        <u-form-item label="税种">
          <u-input v-model="dataForm.th015" disabled></u-input>
        </u-form-item>
        <u-form-item label="税率">
          <u-input v-model="dataForm.th030" disabled></u-input>
        </u-form-item>
        <u-form-item label="供应商单号">
          <u-input v-model="dataForm.th006" disabled></u-input>
        </u-form-item>
        <u-form-item label="厂供料领料更新码">
          <u-input v-model="dataForm.th035" disabled></u-input>
        </u-form-item>
        <u-form-item label="付款条件编号">
          <u-input v-model="dataForm.th033" disabled></u-input>
        </u-form-item>
        <u-form-item label="备注">
          <u-input v-model="dataForm.ti040" disabled></u-input>
        </u-form-item>
      </view>
      <view class="jnpf-table" style="margin-top: 24rpx">
        <view
          class="jnpf-table-item"
          style="margin-bottom: 24rpx"
          v-for="(item, i) in dataForm.moctiList"
          :key="i"
        >
          <u-form-item label="序号">
            {{ i + 1 }}
          </u-form-item>
          <u-form-item label="工单单别">
            <u-input v-model="item.ti013" disabled></u-input>
          </u-form-item>
          <u-form-item label="工单单号">
            <u-input v-model="item.ti014" disabled></u-input>
          </u-form-item>
        </view>
      </view>
      <u-form-item label="原币加工金额">
        <u-input v-model="dataForm.th018" disabled></u-input>
      </u-form-item>
      <u-form-item label="本币税前金额">
        <u-input v-model="dataForm.th031" disabled></u-input>
      </u-form-item>
      <u-form-item label="原币税前货款金额">
        <u-input v-model="dataForm.th027" disabled></u-input>
      </u-form-item>
      <u-form-item label="扣款金额">
        <u-input v-model="dataForm.th019" disabled></u-input>
      </u-form-item>
      <u-form-item label="本币税额">
        <u-input v-model="dataForm.th032" disabled></u-input>
      </u-form-item>
      <u-form-item label="原币税额">
        <u-input v-model="dataForm.th020" disabled></u-input>
      </u-form-item>
      <u-form-item label="总进货数量">
        <u-input v-model="dataForm.th042" disabled></u-input>
      </u-form-item>
      <u-form-item label="总验收数量">
        <u-input v-model="dataForm.th022" disabled></u-input>
      </u-form-item>
    </u-form>
  </view>
</template>

<script>
import { getMocth } from "@/api/workFlow/flowEngine";
import {
  getDataInterfaceDataInfo,
  getDictionaryDataSelector,
  getDataInterfaceRes,
} from "@/api/common";
import comMixin from "../mixin";
import {
  getGoodsList,
  getCustomerList,
  Info,
  Create,
  Update,
} from "@/api/apply/order";
import { getInfo } from "@/api/user";
export default {
  name: "crmOrder",
  mixins: [comMixin],
  data() {
    return {
      current: 0,
      swiperList: [
        {
          name: "发票信息",
        },
        {
          name: "其他信息",
        },
      ],
      hide: false,
      billEnCode: "OrderNumber",
      dataForm: {},
      permissionList: [],
      resultName: "",
      resultRadio: [
        {
          name: "允许",
          id: 1,
          disabled: true,
        },
        {
          name: "拒收",
          id: 2,
          disabled: true,
        },
        {
          name: "其他",
          id: 3,
          disabled: true,
        },
      ],
      resultEndName: "",
      resultEndRadio: [
        {
          name: "拒收",
          id: 1,
          disabled: true,
        },
        {
          name: "特采",
          id: 2,
          disabled: true,
        },
        {
          name: "挑选",
          id: 3,
          disabled: true,
        },
      ],
      lst: [],
      formConf: {},
      id: "",
      buyName: "",
      buyTwoName: "",
      shuizhoLst: [],
      dataForm: {}, //基本信息
      purthList: [], //进货信息
      moctiaList: [], //检测信息列表
      udf14Lst: [], //会签部门字段
    };
  },
  computed: {},
  watch: {
    "dataForm.moctiList": {
      handler(newVal, oldVal) {
        var moctiList = newVal;
        this.calculatePrice(moctiList);
      },
      deep: true,
    },
  },
  methods: {
    init(data, id, flowFormContentJson) {
      this.setting = data;
      this.id = id;
      this.getInfo(data);
    },
    tabsChange(e) {
      this.current = e;
    },
    getInfo(data) {
      getMocth(data.id).then((res) => {
        if (res.code == 200) {
          this.dataForm = res.data;
          //   this.purthList = res.data.purthList;
          this.gettg005(this.dataForm.th005);
          this.getth007(this.dataForm.th007);
          this.getth004(this.dataForm.th004);
          this.getth038(this.dataForm.th038);
          this.getck();
          this.getTh036();
          this.getth012();
          this.getth015();
          this.getth033();
        }
      });
    },
    // 币种
    getth007(id) {
      let data = {
        id,
        interfaceId: "362115609221143109",
        propsValue: "MF001",
        relationField: "JoinStr",
      };
      getDataInterfaceDataInfo("362115609221143109", data).then((res) => {
        if (res.code == 200) {
          this.dataForm.th007 = res.data.JoinStr;
        }
      });
    },
    getth038(id) {
      let data = {
        id,
        interfaceId: "381240499823979141",
        propsValue: "ME001",
        relationField: "ME002",
      };
      getDataInterfaceDataInfo("381240499823979141", data).then((res) => {
        if (res.code == 200) {
          this.dataForm.th038 = res.data.JoinStr;
        }
      });
    },
    getth004(id) {
      let data = {
        id,
        interfaceId: "362111000289221189",
        propsValue: "MB001",
        relationField: "MB002",
      };

      getDataInterfaceDataInfo("362111000289221189", data).then((res) => {
        if (res.code == 200) {
          this.dataForm.th004 = res.data.JoinStr;
        }
      });
    },

    gettg005(id) {
      let data = {
        id,
        interfaceId: "381247065990113925",
        propsValue: "MA001",
        relationField: "JoinStr",
      };
      getDataInterfaceDataInfo("381247065990113925", data).then((res) => {
        if (res.code == 200) {
          this.dataForm.th005 = res.data.JoinStr;
        }
      });
    },
    getck() {
      for (let i = 0; i < this.purthList.length; i++) {
        let data = {
          id: this.purthList[i].th009,
          interfaceId: "381218413038545541",
          propsValue: "MC001",
          relationField: "JoinStr",
        };

        getDataInterfaceDataInfo("381218413038545541", data).then((res) => {
          if (res.code == 200) {
            this.dataForm.purthList[i].th009 = res.data.JoinStr;
          }
        });
      }
    },
    // 签核状态码

    getTh036() {
      getDictionaryDataSelector("393566623006470277").then((res) => {
        const obj = res.data.list.filter(
          (item) => item.enCode == this.dataForm.th036
        )[0];
        this.dataForm.th036 = obj ? obj.fullName : null;
      });
    },
    // 发票种类

    getth012() {
      getDictionaryDataSelector("382030349477096325").then((res) => {
        const obj = res.data.list.filter(
          (item) => item.enCode == this.dataForm.th012
        )[0];
        this.dataForm.th012 = obj ? obj.fullName : null;
      });
    },
    // 税种
    getth015() {
      getDictionaryDataSelector("325937661376201093").then((res) => {
        const obj = res.data.list.filter(
          (item) => item.enCode == this.dataForm.th015
        )[0];
        this.dataForm.th015 = obj ? obj.fullName : null;
      });
    },
    // 付款条件编号
    getth033() {
      getDataInterfaceRes("362115953258928709").then((res) => {
        const obj = res.data.list.filter(
          (item) => item.enCode == this.dataForm.th033
        )[0];
        this.dataForm.th033 = obj ? obj.fullName : null;
      });
    },
    selfGetInfo() {
      Info(this.setting.id).then((res) => {
        this.dataForm = res.data;
        if (this.dataForm.fileJson) {
          this.fileList = JSON.parse(this.dataForm.fileJson);
        }
      });
    },
    selfSubmit() {
      this.dataForm.status = this.eventType === "submit" ? 0 : 1;
      if (this.eventType === "save") return this.selfHandleRequest();
      uni.showModal({
        title: "提示",
        content: "您确定要提交当前流程吗？",
        success: (res) => {
          if (res.confirm) {
            this.selfHandleRequest();
          }
        },
      });
    },
    //汇总到单头
    calculatePrice(moctiList) {
      let money1 = 0; //原币加工金额
      let money2 = 0; //原币扣款金额
      let money3 = 0; //本币进货费用
      let money4 = 0; //本币税前金额
      let money5 = 0; //本币税额
      let money6 = 0; //本币金额合计
      let money7 = 0; //原币税前金额
      let money8 = 0; //原币税额
      let money9 = 0; //原币金额合计
      let money10 = 0; //
      let money11 = 0; //
      for (let i = 0; i < moctiList.length; i++) {
        const e = moctiList[i];
        money1 += parseFloat(e.ti025); //原币加工金额
        money2 += parseFloat(e.ti026); //原币扣款金额
        money3 += parseFloat(e.ti027); //本币进货费用
        money4 += parseFloat(e.ti046); //本币税前金额
        money5 += parseFloat(e.ti047); //本币税额
        money6 = money4 + money5; //本币金额
        money7 += parseFloat(e.ti044); //原币税前金额
        money8 += parseFloat(e.ti045); //原币税额
        money9 = money7 + money8; //原币金额合计
        money10 += parseFloat(e.ti007); //
        money11 += parseFloat(e.ti019); //
      }
      this.dataForm.th018 = parseFloat(money1).toFixed(2);
      this.dataForm.th019 = parseFloat(money2).toFixed(2);
      this.dataForm.th021 = parseFloat(money3).toFixed(2);
      this.dataForm.th031 = parseFloat(money4).toFixed(2);
      this.dataForm.th032 = parseFloat(money5).toFixed(2);
      this.dataForm.sum = parseFloat(money6).toFixed(2);
      this.dataForm.th027 = parseFloat(money7).toFixed(2);
      this.dataForm.th020 = parseFloat(money8).toFixed(2);
      this.dataForm.sumCurrencyLocal = parseFloat(money9).toFixed(2);
      this.dataForm.th042 = parseFloat(money10).toFixed(2);
      this.dataForm.th022 = parseFloat(money11).toFixed(2);
    },
    selfHandleRequest() {
      if (!this.dataForm.id) delete this.dataForm.id;
      const formMethod = this.dataForm.id ? Update : Create;
      formMethod(this.dataForm).then((res) => {
        uni.showToast({
          title: res.msg,
          complete: () => {
            setTimeout(() => {
              uni.$emit("refresh");
              uni.navigateBack();
            }, 1500);
          },
        });
      });
    },
    initData() {
      getGoodsList().then((res) => {
        this.goodsOptions = res.data.list;
      });
      getCustomerList().then((res) => {
        this.customerOptions = res.data.list;
      });
    },
    selfInit() {
      this.addGoods();
      this.addPlan();
    },
    onChange(e) {
      const user = e[0];
      this.dataForm.salesmanName = user.fullName;
    },
    addGoods() {
      const item = {
        goodsId: "",
        goodsCode: "",
        goodsName: "",
        specifications: "",
        unit: "",
        qty: "",
        price: "",
        amount: "",
        discount: null,
        cess: null,
        actualPrice: "",
        actualAmount: "",
        description: "",
      };
      this.dataForm.goodsList.push(item);
    },
    delGoods(index) {
      this.dataForm.goodsList.splice(index, 1);
    },
    addPlan() {
      const item = {
        receivableDate: "",
        receivableRate: "",
        receivableMoney: "",
        receivableMode: "",
        abstract: "",
      };
      this.dataForm.collectionPlanList.push(item);
    },
    delPlan(index) {
      this.dataForm.collectionPlanList.splice(index, 1);
    },
    onGoodsChange(e, i) {
      const goods = e[0];
      this.dataForm.goodsList[i] = {
        goodsId: goods.id,
        goodsCode: goods.code,
        goodsName: goods.text,
        specifications: goods.specifications,
        unit: goods.unit,
        qty: 1,
        price: goods.price,
        amount: goods.price,
        discount: 100,
        cess: 0,
        actualPrice: goods.price,
        actualAmount: goods.price,
        description: "",
      };
      this.$forceUpdate();
    },
    onCustomerChange(e) {
      const customer = e[0];
      this.dataForm.customerName = customer.text;
    },
    count(row) {
      //金额 = 数量*单价
      row.amount = this.jnpf.toDecimal(
        parseFloat(row.price) * parseFloat(row.qty)
      );
      //折扣价 = (单价*折扣)
      var discountPrice = row.price * (row.discount / 100);
      //实际单价 = 折扣价 * (1 + (税率 / 100))
      row.actualPrice = this.jnpf.toDecimal(
        discountPrice * (1 + row.cess / 100)
      );
      //实际金额
      row.actualAmount = this.jnpf.toDecimal(
        parseFloat(row.actualPrice) * parseFloat(row.qty)
      );
      this.$forceUpdate();
    },
  },
};
</script>
